﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml;

namespace TimeRecorder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string DebugFileName = "Test.xml";
        string FileName = "LiveTimeRecord.xml";
        bool Debug = false;
        XmlDocument xmldoc=new XmlDocument();
        public MainWindow()
        {
            InitializeComponent();
            if (Debug)
            {
                FileName = DebugFileName;
            }
            try
            {
                xmldoc.Load(FileName);
            }
            catch 
            { }

            label5.Content = getTotalHours(xmldoc).ToString();

        }
        private double getTotalHours(XmlDocument doc)
        {
            double TotalDuration = 0;
            foreach (XmlNode item in doc.ChildNodes)
            {
                foreach (XmlNode timeRecord in item.ChildNodes)
                {
                    foreach (XmlNode timeRecordAttribute in timeRecord.ChildNodes)
                    {

                            if (timeRecordAttribute.Name == "Duration")
                            {
                                TotalDuration = TotalDuration + Convert.ToDouble(timeRecordAttribute.InnerText);
                            }
                        
                        
                    }   
                }
            }
            return TotalDuration;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!ValidationCheck())
            {
                return;
            }

            if (xmldoc.ChildNodes.Item(1) == null)
            {
                //let's add the XML declaration section
                XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xmldoc.AppendChild(xmlnode);
                //let's add the root element
                XmlNode xmlelem = xmldoc.CreateElement("", "ROOT", "");
                xmldoc.AppendChild(xmlelem);
            }



            XmlNode timeLogNode=xmldoc.CreateElement("","TimeLogEntry","");
            xmldoc.ChildNodes.Item(1).AppendChild(timeLogNode);
            
            XmlNode DateNode=xmldoc.CreateElement("","Date","");
            XmlNode xmltext = xmldoc.CreateTextNode(datePicker1.SelectedDate.ToString());
            DateNode.AppendChild(xmltext);
            timeLogNode.AppendChild(DateNode);

            XmlNode DurationNode = xmldoc.CreateElement("", "Duration", "");
            xmltext = xmldoc.CreateTextNode(textBox1.Text);
            DurationNode.AppendChild(xmltext);
            timeLogNode.AppendChild(DurationNode);

            XmlNode notesNode = xmldoc.CreateElement("", "Notes", "");
            xmltext = xmldoc.CreateTextNode(textBox2.Text);
            notesNode.AppendChild(xmltext);
            timeLogNode.AppendChild(notesNode);

            try
            {
                xmldoc.Save(FileName);
                datePicker1.SelectedDate = null;
                textBox1.Text = "";
                textBox2.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            label5.Content = getTotalHours(xmldoc).ToString();
        }

        private bool ValidationCheck()
        {
            if (datePicker1.SelectedDate == null)
            {
                MessageBox.Show("Invalid Date");
                return false;
            }
            if (textBox1.Text == "")
            {
                MessageBox.Show("No Hours Entered");
                return false;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("No Notes Entered");
                return false;
            }

            return true;
        }


    }
}
